

import java.io.Serializable;
import common.*;

public class Resource implements Serializable{
	private static final long serialVersionUID = 1835443945241704247L;
	private String name;
	private String location;
	private int owner;
	private int granted_for;	// granted task type, either Def.ME_OPR_READONLY or Def.ME_OPR_WRITE
	private int readtime_ms;
	private int writetime_ms;
	private int crc;
	
	/**
	 * Constructor: create a resource (file) record. writetime_ms will be given in random
	 * and readtime_ms will be set proportionally.
	 * ( writetime_ms = [Conf.MIN_WRITETIME_MS,  Conf.MIN_WRITETIME_MS + Conf.MAX_WRITETIME_MS]
	 *   readtime_ms = [.5, 1] * writetime_ms )
	 */
	public Resource( String _name, String _location, int _owner ){
		name = _name;
		location = _location;
		owner = _owner;
		granted_for = Def.NOT_ASSIGNED;
		writetime_ms = ( int )( Math.random() * Conf.MAX_WRITETIME_MS + Conf.MIN_WRITETIME_MS );  // Conf.MIN_WRITETIME_MS  ~  Conf.MIN_WRITETIME_MS + Conf.MAX_WRITETIME_MS  
		readtime_ms = ( int )( Math.random() / 2 + 0.5 * writetime_ms );	// ( 0.5 ~ 1.0 ) * writetime_ms
		crc = ( int )( Math.random() * 99 ) % 64;	// mimic 64-bit CRC
		Utils.print_log( "\"" + location + name + "\" of " + "proc(" + owner + ") / t(rt)=" + readtime_ms + " t(wr)=" + writetime_ms + " granted_for=" + granted_for + " crc=" + crc  );
	}

	// accessors
	public String get_name(){ return name; }
	public String get_location(){ return location; }
	public String get_location_n_name(){ return location + name; }
	public int get_owner(){ return owner; }
	public int get_granted_for(){ return granted_for; }
	public int get_readtime_ms(){ return readtime_ms; }
	public int get_writetime_ms(){ return writetime_ms; }
	public int get_crc(){ return crc; }
	
	// mutator
	public void set_granted_for( int granted_action ){ granted_for = granted_action; } 
}
